<?php
/*
 * @Author: your name
 * @Date: 2020-05-21 17:38:38
 * @LastEditTime: 2020-05-22 16:40:31
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /v3/application/adminapiv3/service/attackLogManage/AttackLog.php
 */ 

namespace app\adminapiv3\service\attackLogManage;

use app\adminapiv3\service\Base;

class AttackLogService extends Base
{
    /**
     * @name: 危险等级转 字符串
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-17 17:15:15
     */
    function dangerLevel2string ($level)
    {
        $data = array_column($this->dangerLevel(),'value','id');
        return isset($data[$level]) ? $data[$level] : '未知' ;
    }

    /**
     * @name: 危险度
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-17 17:17:21
     */
    function dangerLevel ()
    {
        return [
            [
                 'id' => 1,
                 'value' => '低',
            ],
            [
                 'id' => 2,
                 'value' => '中',
            ],
            [
                 'id' => 3,
                 'value' => '高',
            ],
            [
                 'id' => 4,
                 'value' => '极高',
            ],
         ];
    }

    /**
     * @name: 处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-17 17:26:19
     */
    function handle()
    {
        return [
            [
                'id' => 0,
                'value' => '仅记录',
            ],
            [
                'id' => 1,
                'value' => '已阻止',
            ]
        ];
    }

    /**
     * @name: 处理方式转文字
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-17 17:26:19
     */
    function handle2string($handle)
    {
        $data = array_column($this->handle(),'value','id');
        
        return isset($data[$handle]) ? $data[$handle] : '未知' ;
    }

    /**
     * @name: 数据包
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-09 14:58:23
     */
    function dataPackage ($host_id, $event_id, $atime)
    {
        $where['host_id'] = $host_id;
        $where['event_id'] = $event_id;
        $where['atime'] = $atime;

        //查询数据
        $data = db('network_attack_detail')->field('id, data_package')
            ->where($where)->select();

        if(empty($data))
        {
            jsonErrorOut('当前对应主机未找到数据包', 202);
            return [];
        }

        return $data;


    }

    /**
     * @name 当前用户组所管辖主机
     * @author wx
     * @date 2020/7/20 14:27
     */
    function nowGroupHost()
    {
        $user = $this->getUserInfo();
        $return = db('host')->where('group_id', $user['user_group_id'])->column('id');

        return $return;
    }

    /**
     * @name: 所属主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 11:03:02
     */
    function belongedHost()
    {
        $user = $this->getUserInfo();

        //查询出所有下级包括自身id
        $groupIdList = db('server_user_group_relation')->alias('sr')
            ->join('server_user_group sg', 'sg.id = sr.group_id')
            ->where('f_group_id', $user['user_group_id'])->column('group_id');


        $return = db('Host')->whereIn('group_id', $groupIdList)->column('id');

        return $return;
    }
}